package com.lk.labsystem.controller;

//import com.lk.labsystem.config.JwtConfig;
import com.lk.labsystem.pojo.User;
import com.lk.labsystem.service.UserService;
import com.lk.labsystem.utils.ResultOBJ;
import com.lk.labsystem.utils.SYSConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/adminapi/users")
public class UserController {

    @Autowired
    UserService userService;
//    @Autowired
//    JwtConfig jwtConfig;

    //查询用户列表
    @GetMapping
    public List<User> getUsersList(User user) {
        return userService.getUsersList(user);
    }

    //判断登录
    @PostMapping("/login")
    public ResultOBJ login(@RequestBody User user){
        try {
            List<User> list =  userService.getUsersList(user);
            if(list.isEmpty()){
                throw new RuntimeException();
            }
//            User currentUser=list.get(0);
//            String token=jwtConfig.createToken(currentUser.getUsername());
//            currentUser.setToken(token);
//            return new ResultOBJ(SYSConstant.Code_Success,SYSConstant.Login_Success,currentUser);
            return new ResultOBJ(SYSConstant.Code_Success,SYSConstant.Login_Success,list.get(0));
        } catch (Exception e) {
            return new ResultOBJ(SYSConstant.Code_Error,SYSConstant.Login_Error);
        }
    }

//    添加用户
    @PostMapping
    public ResultOBJ addUserList(@RequestBody User user){
        try {
            userService.addUserList(user);
            return ResultOBJ.ADD_SUCCESS;
        } catch (Exception e) {
           return ResultOBJ.ADD_ERROR;
        }
    }

//    更新用户信息
    @PutMapping(value = "/{id}")
    public ResultOBJ updateUserList(@PathVariable Integer id,@RequestBody User user){
        try {
            user.setId(id);
            userService.updateUserList(user);
            return ResultOBJ.UPDATE_SUCCESS;
        } catch (Exception e) {
            return ResultOBJ.UPDATE_ERROR;
        }
    }

//    删除用户
    @DeleteMapping(value = "/{id}")
    public ResultOBJ deleteUserList(@PathVariable Integer id){
        try {
            userService.deleteUserList(id);
            return ResultOBJ.DELETE_SUCCESS;
        } catch (Exception e) {
            return ResultOBJ.DELETE_ERROR;
        }
    }


}
